﻿using System;
using System.Text;

namespace ActivateYourGlutes.Services.Model
{
    public class Goal
    {
        public Account Account { get; set; }

        public int GoalId { get; set; }

        public DateTime CreatedOn { get; set; }

        public double TargetValue { get; set; }

        public bool DecreaseTo { get; set; }

        public DateTime? AchievedOn { get; set; }

        public bool Active { get; set; }

        public DateTime? TargetDate { get; set; }

        public MeasurementType MeasurementType { get; set; }

        public bool Achieved
        {
            get { return AchievedOn != null; }
        }

        public string ToString(UserLocaleSettings localeSettings)
        {
            var sb = new StringBuilder();
            if (DecreaseTo)
                sb.Append("Decrease ");
            else
                sb.Append("Increase ");
            sb.Append(MeasurementType.Description);
            sb.Append(" to ");
            sb.Append(Math.Round(LocalisedValue, 1));
            sb.Append(MeasurementType.Unit);
            if (TargetDate != null)
            {
                sb.Append(" by ");
                sb.Append(((DateTime)TargetDate).ToString(localeSettings.DateFormat));
            }
            if (Achieved)
            {
                sb.Append(" achieved on ");
                sb.Append(((DateTime)AchievedOn).ToString(localeSettings.DateFormat));
            }
            return sb.ToString();
        }

        // TODO: We only need the localised value at the contract level. The target value can be refactoed out.
        public double LocalisedValue
        {
            get
            {
                if (MeasurementType != null)
                    return MeasurementType.LocaliseValue(TargetValue);
                return TargetValue;
            }
            set
            {
                TargetValue = MeasurementType.DelocaliseValue(value);
            }
        }
    }
}